package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api("菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 功能描述: 菜品的分页
     *
     * @return : com.sky.result.Result<com.sky.result.PageResult>
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页")
    public Result<PageResult> page(@ApiParam("菜品DTO") DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 功能描述: 根据菜品分类id查询类型名称,在分页的时候也需要展示菜品分类名称
     *
     * @return : com.sky.result.Result
     */
    @GetMapping("/list")
    @ApiOperation("分类id查询菜品")
    public Result findDishByCategoryId(@ApiParam("分类id") @RequestParam("categoryId") Long categoryId) {
        List<Dish> dishByCategoryId = dishService.findDishByCategoryId(categoryId);
        return Result.success(dishByCategoryId);
    }

    /**
     * 功能描述: 根据菜品id查询菜品信息,判断菜品的状态是否停售
     *
     * @return : com.sky.result.Result
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result findById(@ApiParam("菜品id") @PathVariable Long id) {
        Dish dishServiceById = dishService.findById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishServiceById, dishVO);
        return Result.success(dishVO);
    }

    /**
     * 功能描述: 批量删除
     *
     * @return : com.sky.result.Result
     */
    @ApiOperation("批量删除")
    @DeleteMapping
    public Result delete(@ApiParam("删除DTO") @RequestParam("ids") List<Long> ids) {
        dishService.delete(ids);
        //todo 清除缓存
        String CACHE = "DISH_*";
        //todo redis删除不支持通配符,先要获取keys对应的集合
        Set keys = redisTemplate.keys(CACHE);
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 功能描述: 添加菜品
     *
     * @return : com.sky.result.Result
     */
    @ApiOperation("添加菜品")
    @PostMapping()
    public Result save(@ApiParam("菜品添加DTO") @RequestBody DishDTO dishDTO) {
        dishService.save(dishDTO);
        //todo 清除缓存
        String CACHE = "DISH_" + dishDTO.getCategoryId();
        redisTemplate.delete(CACHE);
        return Result.success();
    }

    /**
     * 功能描述: 菜品停售起售
     *
     * @return : com.sky.result.Result
     */
    @ApiOperation("菜品停售起售")
    @PostMapping("/status/{status}")
    public Result editStatus(@PathVariable Integer status, Long id) {
        dishService.editStatus(status, id);
        //todo 清除缓存
        String CACHE = "DISH_*";
        //todo redis删除不支持通配符,先要获取keys对应的集合
        Set keys = redisTemplate.keys(CACHE);
        redisTemplate.delete(keys);
       // redisTemplate.scan("field", ScanOptions.scanOptions().match("*").count(1000).build());
        return Result.success();
    }

    /**
     * 功能描述: 菜品修改
     *
     * @return : com.sky.result.Result
     */
    @ApiOperation("菜品修改")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        //todo 清除缓存
        String CACHE = "DISH_*";
        //todo redis删除不支持通配符,先要获取keys对应的集合
        Set keys = redisTemplate.keys(CACHE);
        redisTemplate.delete(keys);
        return Result.success();
    }
}
